/********************************************************************
  Mega Mario SRC
    created:	2005/09/18
	created:	18:9:2005   10:13
	author:		Jens Wellmann (c)
*********************************************************************/

#include "Global.h"
#include "MessageBus.h"
#include "CDamageType.h"

cEnemy::cEnemy(int _x,int _y)
{
	this->dir = 0;
	this->x = _x;
	this->y = _y;
	this->y_speed = 0;
	walkcount = 0;
	walkcount2 = 0;
	walkadd = 0;
	this->speed = ENEMYSPEED;
	this->dir = speed;
	this->dead = 0;
	this->DEATHcount = 0;
	this->dying = 0;
}

void cEnemy::update()
{
	if(this->dead)
		return;
	if(this->DEATHcount > 0)
	{
		this->DEATHcount++;
		if(DEATHcount >= 2000)
			this->dead = 1;
	}
	else
	{
		down_touch  = (bool)LEVEL->posValid(x+3,y+3+H) + LEVEL->posValid(x+W-3,y+3+H);
		left_touch  = LEVEL->posValid(x-1,y+H/1.25) + LEVEL->posValid(x-1,y+H/4);
		right_touch = LEVEL->posValid(x + W + 1,y + H / 1.25) + LEVEL->posValid(x + W + 1,y + H / 4);
		if(this->dying)
		{
			down_touch = 0;
			left_touch = 0;
			right_touch = 0;
			if(rect.y >= HEIGHT)
				this->dead = 1;
		}
		else if(PLAYER->x + 40 >= x && PLAYER->x <= x + W)
		{
			if(PLAYER->y + PLAYER->H >= y && PLAYER->y + PLAYER->H <= y + H/2)
				headbang();
		}
		if((left_touch + right_touch)) 
			dir *= -1;
		if(!down_touch)
		{
			y += y_speed;
			if (y_speed < 8)
				y_speed += 0.4f;
		}
		else {
			y_speed = 0;
		}
		x += dir;
		//add_update();
		MakeMyWalk();
	}
	rect.x = (int)x - cam_x;
	rect.y = (int)y - cam_y;
	draw();
}

void cEnemy::die()
{
	this->y -= 40;
	//SCORE->init(x,y,scoring);
	MessageBus::get_MsgBusInstance()->sendMessageFunc<void, int, int, int, const char *>
		(x, y, scoring, "InitScore");
	PLAYSOUND2(S_KICK);
	BLOOD_GENERATOR->newBlood(x,y);
}

SDL_Rect& cEnemy::GetItemRect()
{
	this->rect.x = x - cam_x;
	this->rect.y = y - cam_y;
	return this->rect;
}

float cEnemy::TakeDamage(float DamageAmount, struct CDamageEvent const& DamageEvent, NpcItem* DamageCauser)
 {
	 float ActualDamage = DamageAmount;

	 CDamageType const DamageTypeCDO = DamageEvent.DamageTypeClass;
	 if (DamageEvent.IsOfType(FPointDamageEvent::ClassID))
	 {
		 // point damage event, pass off to helper function
		 FPointDamageEvent* const PointDamageEvent = (FPointDamageEvent*)&DamageEvent;
		 //ActualDamage = InternalTakePointDamage(ActualDamage, *PointDamageEvent, EventInstigator, DamageCauser);

		 // K2 notification for this actor
		 if (ActualDamage != 0.f)
		 {
			 // Notify the component
		 }
	 }
	 else if (DamageEvent.IsOfType(FRadialDamageEvent::ClassID))
	 {
		 // radial damage event, pass off to helper function
		 FRadialDamageEvent* const RadialDamageEvent = (FRadialDamageEvent*)&DamageEvent;
		 //ActualDamage = InternalTakeRadialDamage(ActualDamage, *RadialDamageEvent, EventInstigator, DamageCauser);

		 // K2 notification for this actor
		 if (ActualDamage != 0.f)
		 {
			 // add any desired physics impulses to our components
		 }
	 }

	 // generic damage notifications sent for any damage
	 // note we will broadcast these for negative damage as well
	 if (ActualDamage != 0.f)
	 {

	 }

	 this->die();
     this->dead = 1;

	 return ActualDamage;
 }

 void cEnemy::BeAttacked(float fDamage)
 {

 }
//////////////////////////////////////////////

int cEnemy::playerTouchRight(cPlayer* mario)
{
	if(mario->x + 38 >= x && mario->x + 38 <= x + (W / 2))
	{
		if(mario->y + mario->H >= y + (H/2) && mario->y <= y + H)
			return true;
	}
	return false;
}

int cEnemy::playerTouchLeft(cPlayer* mario)
{
	if(mario->x >= x + W / 2 && mario->x <= x + (W))
	{
		if(mario->y + mario->H >= y + (H/2) && mario->y<= y + H)
			return true;
	}
	return false;
}

void cEnemy::MakeMyWalk()
{
	walkcount2++;
	if(walkcount2 >= 12)
	{
		walkcount = (walkcount) ? 0 : 1;
		walkcount2=0;
	}
}
